package com.zh.system.scheduler;

import com.zh.system.service.SystemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class SystemTask {
    private final SystemService systemService;
    /**
     * 每分钟清理一次过期数据
     */
    @Scheduled(cron = "0 0 0 L * ?")
    public void clearExpireData() {
        int retry = 0;
        int maxRetry = 3;
        while (retry < maxRetry) {
            try {
                systemService.deleteAllExpireData();
                break;
            } catch (InterruptedException e) {
                retry++;
                log.warn("清理过期数据时发生中断异常，正在进行第 {} 次重试", retry);
                Thread.currentThread().interrupt(); // 恢复中断状态
            } catch (Exception e) {
                retry++;
                log.error("清理过期数据时发生异常，正在进行第 {} 次重试: {}", retry, e.getMessage(),e);
            }
        }
        if (retry >= maxRetry) {
            log.error("清理过期数据尝试了 {} 次，仍然失败!!!", maxRetry);
        }
    }
}
